//
//  HomeViewController.swift
//  声网demo
//
//  Created by 秦江涛 on 2022/9/8.
//

import UIKit

class HomeViewController: QIBaseViewController {

    private var dataArr = [HomeListModel]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "分析概览"
        
//        let arr = [1,2,3]
//        print(arr[7])
        addUI()
        getData()
        getStartImageDaat()
    }
    
    private func getData() {
        QINetworkManager.requestJson(urlStr: "/Index/project", method: .get, parameters: [:]) { json in
            print(json)
            self.dataArr = json.arrayObject?.kj.modelArray(HomeListModel.self) ?? [HomeListModel]()
            self.tableView.reloadData()
        }
    }
    
    
    private func addUI() {
        navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
        
//        let testView = videoView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT))
//        QINPopUpTool.sharedInstance().popUp(with: testView, withAnimation: .center, withCanTapBgCancel: false, closeBefore: .node)
        
        let headerImgView = UIImageView().then {
            $0.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 230)
            $0.backgroundColor = UIColor.gradientColor(from: MainColor, to: .white, withHeight: 230)
            $0.image = UIImage(named: "")
            $0.contentMode = .scaleAspectFill
            $0.clipsToBounds = true
        }
        view.addSubview(headerImgView)
        
        view.addSubview(tableView)
    }
    
    
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT)
        tableView.backgroundColor = .clear
        tableView.separatorStyle = .none
        tableView.delegate = self
        tableView.dataSource = self
        if #available(iOS 15.0, *) {
            tableView.sectionHeaderTopPadding = 0
        } else {}
        return tableView
    }()

}

extension HomeViewController: UITableViewDataSource, UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArr.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        180
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = HomeTableViewCell()
        cell.backgroundColor = .clear
        cell.selectionStyle = .none
        cell.model = dataArr[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let ctl = HomeDetailViewController()
        ctl.selectIndex = indexPath.row
        ctl.titleArr = dataArr.map({$0.name})
        navigationController?.pushViewController(ctl, animated: true)
    }
    
}


// 首页其他操作
extension HomeViewController {
    private func getStartImageDaat() {
        QINetworkManager.requestJson(urlStr: "/Config/getStartImg", method: .get, parameters: [:]) { json in
            print(json)
            let imgUrl = "\(IMAGE_URL)\(json.stringValue)"
            UserDefaults.standard.setValue(imgUrl, forKey: "StartDiagram")
        }
    }
}


